//
//  MYGCommonAPI.swift
//  MYDoctor
//
//  Created by Denny on 2024/4/3.
//

import UIKit
import Foundation
import Moya

enum MYGCommonAPI: MYGTargetType {
    
    case uploadOssFiles(image: UIImage?, data: Data? = nil, fileName: String = "", type: String = "image/*")
    case feedbackCreate(tel:String,comment:String,images:String)//反馈意见
    /// 1话题 2会议3视频课程5文献6新闻（报道）7动态(post)8学习资料9临床实验10社区11健康头条12健康大讲堂13医生14医院15药品16疾病百科17问问回答
    case collect(id: Int, type: Int, patient_id: Int? = nil, doctor_id: Int? = nil, collect_status: Int)
    case followAction(id:String,prefix:String = "followUpDoctor")//关注
    case unfollowAction(id:String,prefix:String = "followUpDoctor")//取消关注
}

extension MYGCommonAPI {
    var path: String {
        switch self {
        case .uploadOssFiles:
            return "/api/upload/oss_files"
        case .feedbackCreate:
            return "/feedback/create"
        case .collect:
            return "/api/collect/submit"
        case .followAction:
            return "/api/im/follow"
        case .unfollowAction:
            return "/api/im/nofollow"
        }
    }
    
    var task: Moya.Task {
        switch self {
        case .uploadOssFiles(let image, let data, let fileName, let type):
            var formDatas = [MultipartFormData]()
            if let image = image {
                let imageData = image.compressImage(maxSize: 10*1024*1024-1)!
                let fileName = "\(Date().timeIntervalSince1970).png"
                let formData = MultipartFormData(provider: .data(imageData), name: "file", fileName: fileName, mimeType: "image/*")
                formDatas.append(formData)
                
                let formData2 = MultipartFormData(provider: .data("images".data(using: .utf8)!), name: "dir")
                formDatas.append(formData2)
            }
            
            if let data = data {
                let formData = MultipartFormData(provider: .data(data), name: "file", fileName: fileName, mimeType: type)
                formDatas.append(formData)
            }
            return .uploadMultipart(formDatas)
        case .feedbackCreate(let tel, let comment, let images):
            var formDatas = [MultipartFormData]()
            let telData = MultipartFormData(provider: .data(tel.data(using: .utf8)!), name: "tel")
            formDatas.append(telData)
            
            let commentData = MultipartFormData(provider: .data(comment.data(using: .utf8)!), name: "comment")
            formDatas.append(commentData)

            let imagesData = MultipartFormData(provider: .data(images.data(using: .utf8)!), name: "images")
            formDatas.append(imagesData)

            return .uploadMultipart(formDatas)
        case .collect(id: let id, type: let type, patient_id: let patient_id, doctor_id: let doctor_id, collect_status: let collect_status):
            var params: [String: Any] = [:]
            params["data_id"] = id
            params["type"] = type
            params["patient_id"] = patient_id
            params["doctor_id"] = doctor_id
            params["collect_status"] = collect_status
            return .requestParameters(parameters: params, encoding: JSONEncoding.default)
        case .followAction(let id,let prefix):
            var params: [String: Any] = [:]
            params["id"] = id
            params["prefix"] = prefix
            return .requestParameters(parameters: params, encoding: JSONEncoding.default)
        case .unfollowAction(let id,let prefix):
            var params: [String: Any] = [:]
            params["id"] = id
            params["prefix"] = prefix
            return .requestParameters(parameters: params, encoding: JSONEncoding.default)
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .uploadOssFiles,.feedbackCreate,.collect,.followAction,.unfollowAction:
            .post
        }
    }
    
    var baseURL: URL {
        switch self {
        case
                .uploadOssFiles,.followAction,.unfollowAction:
            return  URL(string: BaseUrl)!
        case .feedbackCreate,.collect:
            return  URL(string: Url)!
        }
    }
    
    var params: [String : Any] {
        switch self {
        case .uploadOssFiles,.feedbackCreate,.collect,.followAction,.unfollowAction:
            return ["":""]
        }
    }

}
